package com.woniu.controller;


import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.QueryRoadDTO;
import com.woniu.pojo.Inspector;
import com.woniu.pojo.Road;
import com.woniu.service.RoadService;
import com.woniu.utils.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "路段管理")
@RestController
@RequestMapping("/road")
@CrossOrigin
public class RoadController extends BaseController {

    @Autowired
    private RoadService roadService;

    @ApiOperation("搜索+分页")
    @PostMapping("/list/search/{currentPage}/{pageSize}")
    public Result listSearch(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody(required = false) QueryRoadDTO queryRoadDTO){
        PageData pageData =  roadService.listSearch(currentPage, pageSize, queryRoadDTO);
        return toDataResult(pageData);
    }

    @ApiOperation("新增或者修改")
    @PostMapping
    public Result saveOrUpdate(@RequestBody Road road){
        boolean flag = roadService.saveOrUpdate(road);
        return toResult(flag);
    }

    @ApiOperation("删除操作")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        boolean flag = roadService.removeById(id);
        return toResult(flag);
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id){
        Road road = roadService.getById(id);
        return toDataResult(road);
    }

    @ApiOperation("根据ID查询巡检员姓名")
    @GetMapping("/inspector/{id}")
    public Result getByInspectorName(@PathVariable String id){
        Inspector inspector = roadService.getByInspectorName(id);
        return toDataResult(inspector);
    }

}
